<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript" src="<c:url value="/js/plug/area/load_area.js" />"></script>
<style>
.detailTable input[type="text"]{
	width: 210px;
}
</style>
<div class="easyui-layout" style="width:100%;height:100%;" fit="true">
    <div data-options="region:'center',title:'',split:true" style="height:100%;">
		<form id="salesmanInfoForm" method="post" >
		    <input type="hidden" id="salesmanId" name="salesmanId" data-options="required:true">
			<table class="detailTable"> 
    	 	<tr>
                <th>姓名:</th> 
                <td ><input class="easyui-validatebox " id="salesmanName" name="salesmanName" data-options="required:true"></td>
                <th>性别:</th>
                <td>
                    <input type="radio"  name="salesmanSex" value="1" checked="checked">男
					<input type="radio"  name="salesmanSex" value="2" >女
               </td>
            </tr>
            <tr>
               <th>职务:</th><td colspan="3"><input class="easyui-validatebox" id="duty"name="duty"></td>
            </tr>
            <tr>
                <th>类型:</th>
                <td colspan="3">
                    <input type="radio"  name="salesmanType" value="1" >厂家业务员
					<input type="radio"  name="salesmanType" value="2" checked="checked">公司业务员
               </td>
            </tr>
            <tr id= "aa" style="display: none">
             <th>供应商:</th>
             <td colspan="3" >
				<input type="text" name="providerName" id="d_providerName" readonly="readonly" class="easyui-validatebox" style="width: 250px;">
				<input type="hidden" name="providerCode" id="d_providerCode" >
				<a href="#" onclick="chooseProvider()" class="easyui-linkbutton" data-options="iconCls:'icon-add'">选择</a>
			</td>
			</tr>
			<tr id= "bb" >
			  <th></th>
			  <td colspan="3">
               		<span style="color: red;font-weight: bold;">是否能代理供应商选择:</span>
                    <input type="radio"  name="isAgentProvider" value="0" >否
					<input type="radio"  name="isAgentProvider" value="1">是
					<br><br>
					<span style="color: red;font-weight: bold;">身份代理选择:</span>
				 	<input type="radio" id="addBuyer" name="addBuyer" value="1" >所有客户
					<input type="radio" id="addBuyer" name="addBuyer" value="2" >所属客户
               </td>
            </tr>
             <tr>
                <th>祖籍:</th>
                <td><input class="easyui-validatebox" id="salesmanNative" name="salesmanNative"></td>
                <th>可见区域:</th>
                <td>
                 <div id="loc" >
                   <select name="province" id="province" style="width:100px;">
                     <option value="0">选择省</option>
                   </select>   
                   <select name="city" id="city" style="width:100px;">
                     <option value="0">选择市</option>
                   </select>   
                   <select name="district" id="district" style="width:100px;">
                     <option value="0">选择地区</option>
                   </select>
                  </div>
                  <input type="hidden" id="seeArea" name="seeArea" data-options="required:true" >
            </tr>
            
            <tr>
                <th>电话:</th>
                <td><input class="easyui-validatebox " id="phone" name="phone" data-options="validType:'phone'"></td>
                <th>手机:</th>
                <td><input class="easyui-validatebox " id="mobile" name="mobile" data-options="validType:'mobile'"></td>
            </tr>
            <tr>
                <th>邮箱:</th>
                <td><input class="easyui-validatebox " id="email" name="email" data-options="validType:'email'"></td>
                <th>QQ:</th>
                <td><input class="easyui-validatebox " id="qq" name="qq" data-options="validType:'qq'"></td>
            </tr>
            <tr>
                <th>备注:</th>
                <td colspan="3" ><input class="easyui-validatebox" id="remark" name="remark"  style="width:400px;" ></td>
            </tr>
            <tr>
                <th>部门编码:</th>
                <td><input class="easyui-validatebox " id="deptCode" name="deptCode"></td>
                <th>部门:</th>
                <td><input class="easyui-validatebox " id="department" name="department"></td>
            </tr>
            <tr>
                <th>加入日期:</th>
                <td><input class="easyui-datebox " id="joinDate" name="joinDate"></td>
                <th>会员等级:</th>
                <td>
				<select name="modifyPrice" id="modifyPrice" >
					<option value="1">可修改价格</option>
					<option value="2">可选批次</option>
					<option value="3">可修改价格和批次</option>
					<option value="4">可调配送价</option>
				</select>
               </td> 
            </tr>
            
			<tr>
			    <th>操作：</th>
				<td align="center" colspan="3" > 
					<input type="button" onclick="submitForm()" class="button" value="提交" >
					<input type="button" onclick="pms.resetForm('salesmanInfoForm')" class="button" value="重置" >
				 	<!-- <input type="button" onclick="test()" class="button" value="测试" >     -->   
				</td>
			</tr>
		</table>
		</form>
    </div>
</div>  
<script>

var flag =0; // 标示 0：save方法，1：update方法
$(function(){
	var salesmanId="<%=request.getParameter("code")%>";
	if(salesmanId=="null"){	//save	
		$("#loc").LocationSelect({
			data:"<c:url value='/js/plug/area/areas.json' />"
		}); 
		$("tr[id='aa']").attr("type","hidden");
	}else{					//update
		flag = 1;
		var param = {"salesmanId":salesmanId};
 		$.pmsAjax({
			url:'<c:url value="/salesman/showEmBSalesmanDetail.do"/>',
			data:param,
			success:function(data){
				var emBSalesman = data.data.emBSalesman;				
				if(data.code=="200"){
					//alertShow("成功");
					$('#salesmanInfoForm').form('load', emBSalesman);
					$("#loc").LocationSelect({
						data:"<c:url value='/js/plug/area/areas.json' />",
						areaCode:emBSalesman.seeArea
					});
					if(emBSalesman.salesmanType=="1"){
						$('input:radio[name=salesmanType]')[0].click();
					}
					if(emBSalesman.salesmanType=="2"){
						$('input:radio[name=salesmanType]')[1].click();
					}
				}else{
					alertError(data.result);
				}	
			}
		});
	}
	
	
	//选中用户类型
	$('input:radio[name=salesmanType]').click(function(){
			if($(this).val()==1){
				$("#aa").show();
				$("#bb").hide();
			}
			if($(this).val()==2){
				$("#aa").hide();
				$("#bb").show();
			}
		
	});
});
function submitForm(){
	if(flag == 0){
		doInsert();
	}else{
		doUpdate();
	}	
}
//insert
function doInsert(){
	$('#salesmanInfoForm').form('submit', {
		url:'<c:url value="/salesman/insertSalesman.do"/>',
	    onSubmit: function(param){
	    	if(!$("#salesmanInfoForm").form('validate')){
	    		return false;
	    	}
	    	var area = $("#loc").getLocation('all').code;
	    	$("#seeArea").val(area);
	    }, 
	    success:function(data){
		    var ret = eval("("+data+")");
		    console.log(ret);
	     	if(ret.code=='200'){
	     		parent.alertShow("新增成功");
	     		parent.salesmansTable.refresh();
	     		parent.$("#w_salesman_add").dialog('close');
		    }else{
		    	parent.alertError(ret.result);
			}
	    }
	});
}
//update
function doUpdate(){
	$('#salesmanInfoForm').form('submit', {
	    url:'<c:url value="/salesman/updateSalesman.do"/>',
	    onSubmit: function(param){
	    	if(!$("#salesmanInfoForm").form('validate')){
	    		return false;
	    	}
	    	var area = $("#loc").getLocation('all').code;
	    	var name = $("#loc").getLocation('all').name;
	    	$("#seeArea").val(area);
	    	//console.log(name);
	    	//时间对比
	    }, 
	    success:function(data){
		    var ret = eval("("+data+")");
		    console.log(ret);
	     	if(ret.code=='200'){
	     		parent.alertShow("修改成功");
	     		parent.salesmansTable.refresh();
	     		parent.$("#w_salesman_update").dialog('close');
	     		
		    }else{
		    	alertError(ret.result);
			}
	    }
	});
}
function chooseProvider(){
	$.pmsDialog({
		id:"w_chooseProvider",
		title:'供货商选择',
		method:'post',
		isIf:'true',
		href:'<c:url value="/views/jsp/customer/provider/provider_list_forchoose_s.jsp"/>',
		width:900,
	});
}
//选择供应商
function getProvider(providerName,providerCode){
	$("#d_providerCode").val(providerCode);
	$("#d_providerName").val(providerName);
	$("#w_chooseProvider").dialog('close');
}
function test(){
	$("#salesmanName").val("测试业务员");
	$("#salesmanNative").val("浙江杭州");

	$("#phone").val("0570-88661035");
	$("#mobile").val("15300001111");
	$("#email").val("676768767@qq.com");
	$("#qq").val("676768767");
	
	$("#duty").val("销售");
	$("#deptCode").val("ZA1100");
	$("#department").val("销售一部");
	$("#remark").val("测试新增业务员");
	
	}
</script>
